const mongoose = require('mongoose')

mongoose.connect('mongodb://localhost/test', { //连接数据库
    useUnifiedTopology: true
}).then(() => {
    console.log('数据库连接成功！')
}).catch(err => {
    console.log(err)
})
//创建Schema规则
const empSchema = new mongoose.Schema({ //定义字段规则
    status: {
        type: Number,
        default: 200
    },
    message: {
        type: String,
        default: 'ok'
    },
    data: [{
        id: {
            type: String,
            default: new Date() + ''
        },
        name: {
            type: String,
            require: true,
            minlength: 2,
            maxlength: 10,
        },
        sex: String,
        age: {
            type: Number,
            min: 0,
            max: 120
        },
        email: {
            type: String,
            require: true,
        },
        phone: {
            type: String,
            require: true,
        },
        role: {
            rl_name: {
                type: String,
            },
            power: {
                type: String,
            },
            level: {
                type: Number,
                default: 0
            },
            cangoto: {
                type: String
            }
        },
        enter_time: {
            type: Date,
            default: Date.now
        }
    }]
})
//使用规则创建集合
const emp = mongoose.model('Emp', empSchema, 'emp')
//定义集合要写入的数据

// emp.find().select('data').then(result => {
//     console.log(result);
// })


module.exports = {
    emp
}